﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using PowerSong.SongDatabase;
    using System.Text;

    /// <summary>
    /// Interaction logic for EditStyleWindow.xaml
    /// </summary>
    public partial class EditStyleWindow : Window
        {

        private SongDatabase.Style style = null;

        private Theatre theatre = null;

        private Song song = null;

        public EditStyleWindow(SongDatabase.Style existingStyle)
            {

            InitializeComponent();

            // Create the sample song
            this.song = new Song("Song Title Here") { Copyright = "Copyright Information Here" };
            var verseNames = new string[] { "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth" };
            var lineNames = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
            for (int i = 1; i <= verseNames.Length; i++)
                {
                var verseText = new StringBuilder();
                for (int j = 0; j < 2 + i; j++)
                    {
                    if (j > 0) verseText.AppendLine();
                    verseText.Append(string.Format("{0} Verse Line {1}", verseNames[i - 1], lineNames[j]));
                    }
                this.song.Verses.Add(verseText.ToString());
                }

            // Clone the style so that it can be edited
            this.style = new PowerSong.SongDatabase.Style();
            existingStyle.CopyTo(style);
            style.ID = existingStyle.ID;

            // Set the name control
            this.NameTextBox.Text = style.Name;

            // Set the background controls
            var backgroundColor = existingStyle.BackgroundColor.ToColor();
            BackgroundColor.Brush = new SolidColorBrush(backgroundColor);
            if (!string.IsNullOrWhiteSpace(existingStyle.BackgroundFileName))
                {
                this.BackgroundImageRadioButton.IsChecked = true;
                this.ImageFileName.FileName = existingStyle.BackgroundFileName;
                }
            else
                {
                this.BackgroundColorRadioButton.IsChecked = true;
                }

            // Set the substyle controls
            TitleTextPreview.SetSubStyle(backgroundColor, existingStyle.GetSubStyle("Title"));
            VerseTextPreview.SetSubStyle(backgroundColor, existingStyle.GetSubStyle("Verse"));
            CopyrightTextPreview.SetSubStyle(backgroundColor, existingStyle.GetSubStyle("Copyright"));

            // Create a cinema
            this.theatre = new Theatre();
            this.theatre.CreateProjector(this.ProjectorPreview);

            }

        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // Ensure a name for the style has been specified
                if (string.IsNullOrEmpty(NameTextBox.Text))
                    {
                    throw new Exception("Please specify a name for the style.");
                    }

                // Update style details
                style.Name = NameTextBox.Text;
                State.Database.Styles.Write(style);

                // Close the window
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Error Saving Style Details", ex.Message);
                }

            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }

        private void UpdateBackgroundPreview()
            {
            try
                {

                UIElement control = null;
                BackgroundPreviewBorder.Background = Brushes.Black;

                if (BackgroundColorRadioButton.IsChecked == true)
                    {

                    // Just set the background colour
                    BackgroundPreviewBorder.Background = BackgroundColor.Brush;

                    }
                else if (BackgroundImageRadioButton.IsChecked == true)
                    {

                    // Create a preview of the background image
                    var image = new Image();
                    image.Stretch = Stretch.Fill;
                    image.Source = new BitmapImage(new Uri(ImageFileName.FileName, UriKind.RelativeOrAbsolute));
                    control = image;

                    }
                else
                    {

                    // Create a preview of the background video
                    var video = new MediaElement();
                    video.LoadedBehavior = MediaState.Play;
                    video.Stretch = Stretch.Fill;
                    video.Source = new Uri(VideoFileName.FileName, UriKind.RelativeOrAbsolute);
                    video.IsMuted = true;
                    control = video;

                    }

                BackgroundPreviewBorder.Child = control;
                }
            catch { }
            }

        private void BackgroundTypeChanged(object sender, RoutedEventArgs e)
            {
            BackgroundColor.IsEnabled = BackgroundColorRadioButton.IsChecked == true;
            ImageFileName.IsEnabled = BackgroundImageRadioButton.IsChecked == true;
            VideoFileName.IsEnabled = BackgroundVideoRadioButton.IsChecked == true;
            VolumeSlider.IsEnabled = BackgroundVideoRadioButton.IsChecked == true;
            MuteAudioCheckBox.IsEnabled = BackgroundVideoRadioButton.IsChecked == true;
            UpdateBackgroundPreview();
            }

        private void VideoFileName_FileNameChanged(object sender, EventArgs e)
            {
            UpdateBackgroundPreview();
            }

        private void ImageFileName_FileNameChanged(object sender, EventArgs e)
            {
            UpdateBackgroundPreview();
            }

        private void MuteAudioCheckBox_Checked(object sender, RoutedEventArgs e)
            {
            VolumeSlider.IsEnabled = false;
            }

        private void MuteAudioCheckBox_Unchecked(object sender, RoutedEventArgs e)
            {
            VolumeSlider.IsEnabled = true;
            }

        private void EditTitleButton_Click(object sender, RoutedEventArgs e)
            {
            new EditStyleElementWindow(theatre, style, style.GetSubStyle("Title")).ShowDialog();
            }

        private void EditVerseButton_Click(object sender, RoutedEventArgs e)
            {
            new EditStyleElementWindow(theatre, style, style.GetSubStyle("Verse")).ShowDialog();
            }

        private void EditCopyrightButton_Click(object sender, RoutedEventArgs e)
            {
            new EditStyleElementWindow(theatre, style, style.GetSubStyle("Copyright")).ShowDialog();
            }

        private void FadeOutButton_Click(object sender, RoutedEventArgs e)
            {
            theatre.ChangeMode(ProjectorMode.Black);
            }

        private void FadeInButton_Click(object sender, RoutedEventArgs e)
            {
            theatre.ChangeMode(ProjectorMode.Full);
            }

        private void CrossFadeButton_Click(object sender, RoutedEventArgs e)
            {
            theatre.ChangeVerse((theatre.VerseIndex + 1) % song.Verses.Count);
            }

        private void StyleTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
            if (StyleTabControl.SelectedItem == PreviewTabItem)
                {
                UpdateStyle();
                this.theatre.ChangeText(this.song.Title, this.song.Verses.ToArray(), this.song.Copyright);
                this.theatre.ChangeStyle(this.style);
                this.theatre.ChangeMode(ProjectorMode.Full);
                }
            }

        private void UpdateStyle()
            {
            style.Name = NameTextBox.Text;
            style.BackgroundFileName = string.Empty;
            if (BackgroundColorRadioButton.IsChecked == true)
                {
                style.BackgroundColor = BackgroundColor.Color.ToUInt();
                }
            else
                {
                style.BackgroundFileName = ImageFileName.FileName;
                }
            }

        }

    }
